package club.banyuan;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Enumeration;

@WebServlet(name = "Servlet2", urlPatterns = "/s2")
public class Servlet2 extends HttpServlet {
    @Override
    public void init() throws ServletException {
        super.init();
        System.out.println("Servlet init");
    }

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        super.service(req, res);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 互斥
        String username = request.getParameter("username");
        System.out.println("username: " + username);

        BufferedReader reader = request.getReader();
//        BufferedReader reader1 = new BufferedReader(new InputStreamReader(request.getInputStream()));
        String str;
        while ((str = reader.readLine()) != null && str.length() > 0) {
            System.out.println(str);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getMethod();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String queryString = request.getQueryString();
        String url = request.getRequestURL().toString();
        String uri = request.getRequestURI();
        String servletPath = request.getServletPath();
        String contextPath = request.getContextPath();
        String userAgent = request.getHeader("User-Agent");
        String Connection = request.getHeader("Connection");

        response.getWriter().println("method: " + method);
        response.getWriter().println("username: " + username);
        response.getWriter().println("password: " + password);
        response.getWriter().println("query String: " + queryString);
        response.getWriter().println("url: " + url);
        response.getWriter().println("uri: " + uri);
        response.getWriter().println("servletPath: " + servletPath);
        response.getWriter().println("contextPath: " + contextPath);
        response.getWriter().println("userAgent: " + userAgent);
        response.getWriter().println("Connection: " + Connection);

        HttpSession session = request.getSession();
        // 设置session属性
        session.setAttribute("name", "s2");

        Enumeration<String> names = request.getHeaderNames();
        while (names.hasMoreElements()) {
            String name = names.nextElement();
            String value = request.getHeader(name);
            System.out.println("name: " + name + "value: " + value);
        }
    }

    @Override
    public void destroy() {
        super.destroy();
        System.out.println("Servlet destroy");
    }
}
